
#include "../inc/debug.h"
#include "../inc/include_other.h"
#include "../inc/main.h"
//*******************************************************************************************

static lv_obj_t*	   jpeg_obj     = NULL;
static lv_obj_t*	   jpeg_img_obj = NULL;
static lv_img_dsc_t*   jpeg_dsc_data  = NULL;

static uint8_t  *	   jpeg_img_bff = NULL;

//*******************************************************************************************

int32_t jpeg_show_test(void)
{
	int bw = 0;
	int bh = 0;
	int bpp = 0;

	lv_coord_t x = 0;
	lv_coord_t y = 0;

	lv_coord_t w = 0;
	lv_coord_t h = 0;

	if(jpeg_obj)
		return 0;

	lv_obj_set_style_bg_opa(lv_scr_act(), LV_OPA_0, LV_PART_MAIN);

	w = lv_disp_get_hor_res(NULL);
	h = lv_disp_get_ver_res(NULL);



	jpeg_obj = lv_obj_create(lv_scr_act());
	lv_obj_set_style_radius(jpeg_obj, 0, LV_PART_MAIN);
	lv_obj_set_style_border_width(jpeg_obj, 0, LV_PART_MAIN);
	lv_obj_set_style_border_color(jpeg_obj, lv_color_make(0x00, 0x00, 0x00), LV_PART_MAIN);
	lv_obj_set_style_border_opa(jpeg_obj, LV_OPA_0, LV_PART_MAIN);

	lv_obj_set_style_outline_width(jpeg_obj, 0, LV_PART_MAIN);
	lv_obj_set_style_shadow_width(jpeg_obj, 0, LV_PART_MAIN);
	lv_obj_set_scrollbar_mode(jpeg_obj, LV_SCROLLBAR_MODE_OFF);

	
	lv_obj_set_style_pad_all(jpeg_obj, 0,LV_PART_MAIN);
	
#if 0
	lv_obj_set_style_bg_opa(jpeg_obj, LV_OPA_0, LV_PART_MAIN);
#else
	lv_obj_set_style_bg_opa(jpeg_obj, LV_OPA_100, LV_PART_MAIN);
	lv_obj_set_style_bg_color(jpeg_obj, lv_color_make(0x00, 0x00, 0xFF), LV_PART_MAIN);
#endif
	
	lv_obj_set_pos(jpeg_obj, 0, 0);
	lv_obj_set_size(jpeg_obj, w, h);
	//*******************************************************************************************


	jpeg_img_obj = lv_img_create(jpeg_obj);

	x = 0;
	y = 0;
	
	debug_d("pw=%d\n",w);
	debug_d("ph=%d\n",h);
	
	lv_obj_set_pos(jpeg_img_obj,x,y);
	lv_obj_set_size(jpeg_img_obj,w,h);

	//*******************************************************************************************

	int ret = 0;

	struct jpeg_decompress_struct cinfo;
	 struct jpeg_error_mgr jerr;
	
	 FILE *infile;
	 JSAMPARRAY buffer;
	 int row_stride;
	
	 if ((infile = fopen("./2.jpg", "rb")) == NULL) {
		 fprintf(stderr, "Can't open input file\n");
		 return 0;
	 }
	
	 cinfo.err = jpeg_std_error(&jerr);

	 jpeg_create_decompress(&cinfo);

	 cinfo.scale_num = 1;//2

	 cinfo.scale_denom = 1;
	 
	 //cinfo.out_color_space = JCS_RGB;

	 
	 jpeg_stdio_src(&cinfo, infile);
	 jpeg_read_header(&cinfo, TRUE);
	 jpeg_start_decompress(&cinfo);
	
	 row_stride = cinfo.output_width * cinfo.output_components;
	 buffer = (*cinfo.mem->alloc_sarray)((j_common_ptr)&cinfo, JPOOL_IMAGE, row_stride, 1);

	 debug_d("w=%d\n",cinfo.output_width);
	 debug_d("h=%d\n", cinfo.output_height);
	 debug_d("scanline=%d\n", cinfo.output_scanline);
	 debug_d("output_components=%d\n", cinfo.output_components);

	 int row_size = cinfo.output_width * cinfo.output_components;
	 int Bpp      = cinfo.output_components;
	 int size     = row_size * cinfo.output_height;

	 bw   = cinfo.output_width;
	 bh   = cinfo.output_height;
	 bpp  = cinfo.output_components;

	 debug_d("row_size=%d\n", row_size);
	 debug_d("Bpp=%d\n", Bpp);
	 debug_d("size=%d\n", size);

	 uint8_t* jpeg_buff = NULL;
	 uint8_t* jpeg_buff_temp = NULL;

	 jpeg_buff = (uint8_t *)malloc(size);

	 memset(jpeg_buff,0,size);

	jpeg_img_bff = jpeg_buff;
	
	 debug_d("jpeg_buff1=%x\n", jpeg_buff);
	
	 while (cinfo.output_scanline < cinfo.output_height) {
		 jpeg_read_scanlines(&cinfo, buffer, 1);
		 // 在这里可以处理解码后的图像数据，例如保存到文件或进行其他操作

	 	memcpy(jpeg_buff , buffer, row_size);
	 	jpeg_buff = jpeg_buff + row_size;

	 }
	 
	 debug_d("jpeg_buff2=%x\n", jpeg_buff);
	 debug_d("jpeg_buff3=%x\n", jpeg_img_bff);
	
	 jpeg_finish_decompress(&cinfo);
	 jpeg_destroy_decompress(&cinfo);
	 fclose(infile);

	 //*******************************************************************************************

	debug_d("bw=%d\n",bw);
	debug_d("bh=%d\n",bh);
	debug_d("bpp=%d\n",bpp);

	
    jpeg_dsc_data = (lv_img_dsc_t*)malloc(sizeof(lv_img_dsc_t));

    jpeg_dsc_data->header.always_zero = 0;
    jpeg_dsc_data->header.cf = LV_IMG_CF_TRUE_COLOR;//LV_IMG_CF_RAW;//LV_IMG_CF_TRUE_COLOR;
    jpeg_dsc_data->header.w = bw;
    jpeg_dsc_data->header.h = bh;
    jpeg_dsc_data->data_size = bw*bh*bpp ;
    jpeg_dsc_data->data = (const uint8_t*)jpeg_img_bff;


	lv_img_set_src(jpeg_img_obj,jpeg_dsc_data);
	//*******************************************************************************************

	return 0;
}

